import client from './client'
import type { ApiResponse } from '@/types/api'

export interface AdminInfo {
  id: number
  username: string
  adminRole: string
  adminRoleId: number
  createdAt?: string
  updatedAt?: string
}

export interface AdminCreateRequest {
  username: string
  password: string
  adminRole: string
  adminRoleId: number
}

export interface AdminUpdateRequest {
  id: number
  username: string
  adminRole: string
  adminRoleId: number
}

export interface AdminResetPasswordRequest {
  id: number
  newPassword: string
}

/**
 * 管理员管理服务类（仅超级管理员可用）
 */
export class AdminManageService {
  /**
   * 获取所有管理员列表
   */
  static async getAdminList(): Promise<ApiResponse<AdminInfo[]>> {
    console.log('🔍 [AdminManageService] 获取管理员列表')
    
    try {
      const response = await client.get('/api/admin/manage/list')
      console.log('✅ [AdminManageService] 获取管理员列表成功:', response.data)
      return response.data as ApiResponse<AdminInfo[]>
    } catch (error: any) {
      console.error('❌ [AdminManageService] 获取管理员列表失败:', error)
      throw error
    }
  }

  /**
   * 根据ID获取管理员详情
   */
  static async getAdminById(id: number): Promise<ApiResponse<AdminInfo>> {
    console.log(`🔍 [AdminManageService] 获取管理员详情, ID: ${id}`)
    
    try {
      const response = await client.get(`/api/admin/manage/${id}`)
      console.log('✅ [AdminManageService] 获取管理员详情成功:', response.data)
      return response.data as ApiResponse<AdminInfo>
    } catch (error: any) {
      console.error('❌ [AdminManageService] 获取管理员详情失败:', error)
      throw error
    }
  }

  /**
   * 创建新管理员
   */
  static async createAdmin(data: AdminCreateRequest): Promise<ApiResponse<boolean>> {
    console.log('🔍 [AdminManageService] 创建管理员:', { ...data, password: '***' })
    
    try {
      const response = await client.post('/api/admin/manage/create', data)
      console.log('✅ [AdminManageService] 创建管理员成功:', response.data)
      return response.data as ApiResponse<boolean>
    } catch (error: any) {
      console.error('❌ [AdminManageService] 创建管理员失败:', error)
      throw error
    }
  }

  /**
   * 更新管理员信息
   */
  static async updateAdmin(data: AdminUpdateRequest): Promise<ApiResponse<boolean>> {
    console.log('🔍 [AdminManageService] 更新管理员信息:', data)
    
    try {
      const response = await client.put('/api/admin/manage/update', data)
      console.log('✅ [AdminManageService] 获取管理员信息成功:', response.data)
      return response.data as ApiResponse<boolean>
    } catch (error: any) {
      console.error('❌ [AdminManageService] 更新管理员信息失败:', error)
      throw error
    }
  }

  /**
   * 删除管理员
   */
  static async deleteAdmin(id: number): Promise<ApiResponse<boolean>> {
    console.log(`🔍 [AdminManageService] 删除管理员, ID: ${id}`)
    
    try {
      const response = await client.delete(`/api/admin/manage/${id}`)
      console.log('✅ [AdminManageService] 删除管理员成功:', response.data)
      return response.data as ApiResponse<boolean>
    } catch (error: any) {
      console.error('❌ [AdminManageService] 删除管理员失败:', error)
      throw error
    }
  }

  /**
   * 重置管理员密码
   */
  static async resetAdminPassword(data: AdminResetPasswordRequest): Promise<ApiResponse<boolean>> {
    console.log('🔍 [AdminManageService] 重置管理员密码:', { ...data, newPassword: '***' })
    
    try {
      const response = await client.post('/api/admin/manage/reset-password', data)
      console.log('✅ [AdminManageService] 重置管理员密码成功:', response.data)
      return response.data as ApiResponse<boolean>
    } catch (error: any) {
      console.error('❌ [AdminManageService] 重置管理员密码失败:', error)
      throw error
    }
  }
}

export default AdminManageService
